<?php
function mangaf1_url_manga($id,$str){
	
		//return HOME."index.php?module=truyen&act=view&id=".$id;
		return HOME.'truyentranh/'.$id.'/'.$str.'.html';
	
}
function mangaf1_url_manga_theloai($id,$str){
	
		return HOME.'truyentranh/'.$str.".html";
	
}
function mangaf1_url_manga_nguon($str){
	
		return HOME.'truyentranh/group/'.$str.".html";
	
}
function mangaf1_url_manga_episode($id,$str1,$str2=''){
//return HOME."index.php?module=truyen&act=chuong&id=".$id;
return HOME.'truyentranh/'.$id.'/'.$str1.'/';

}

function mangaf1_url_manga_tag($str){
		return HOME."tag/".khongdau(trim($str)).".html";
		//return HOME."index.php?module=truyen&act=tacgia&val=".khongdau($str);
	
}
function mangaf1_url_manga_tag2($str){
		return HOME."tag2/".khongdau(trim($str)).".html";
		//return HOME."index.php?module=truyen&act=tacgia&val=".khongdau($str);
	
}
function mangaf1_url_manga_tag3($str){
		return HOME."tag3/".khongdau(trim($str)).".html";
		//return HOME."index.php?module=truyen&act=tacgia&val=".khongdau($str);
	
}
function mangaf1_url_manga_tag4($str){
		return HOME."tag4/".khongdau(trim($str)).".html";
		//return HOME."index.php?module=truyen&act=tacgia&val=".khongdau($str);
	
}
function mangaf1_gglink($link){
return preg_replace('/https:\/\/(.*?)googleusercontent\.com/', 'http://3.bp.blogspot.com', $link);
}

//Tags function-----------

function mangaf1_tag1_generate($str){
//Genrate tag from anime name
$string="truyen tranh nguoi lon,truyen tranh hay,truyen tranh vui,truyen tranh 18+,truyen tranh nhat ban,truyen tranh han quoc,doc truyen tranh online,doc truyen tranh tinh cam,doc truyen tranh online tieng viet,doc truyen tranh online nhanh,doc truyen tranh online 16+,doc truyen tranh online 18+,doc truyen $str tieng viet,truyen $str full,doc truyen $str tron bo,$str full,$str tron bo,truyen tranh nguoi lon nhat ban";
return $string;
}
function mangaf1_tag1_link($str){
$string="truyen tranh nguoi lon,truyen tranh hay,truyen tranh vui,truyen tranh 18+,truyen tranh nhat ban,truyen tranh han quoc,doc truyen tranh online,doc truyen tranh tinh cam,doc truyen tranh online tieng viet,doc truyen tranh online nhanh,doc truyen tranh online 16+,doc truyen tranh online 18+,doc truyen $str tieng viet,truyen $str full,doc truyen $str tron bo,$str full,$str tron bo,truyen tranh nguoi lon nhat ban";
	if($string){
					
					
							$tagar=explode(",",$string);
								$tag="";
										foreach($tagar as $value ){
											$value=trim($value);
											$tag.="<a href='".mangaf1_url_manga_tag(khongdau($value))."'>$value</a> ,";
																								
										}
										
					return substr($tag,0,strlen($tag)-1);
							
				}

}
function mangaf1_tag2_generate($str,$str2,$tt){
	$f=strpos($str2,$tt);
	if($f===false){
	//Not find thutu in chapter_name
	$string="$str $str2,$str $str2 tieng viet";
	}else{
	$string="$str  chap $tt,$str chap $tt tieng viet,doc truyen $str chap $tt tieng viet online";
	}
	
	
	
return $string;
}

function mangaf1_tag2_generates($str,$tt){

	$string="str chap $tt tieng viet,doc truyen $str chap $tt tieng viet online";
	
	
	
	
return $string;
}
function mangaf1_tag2_link($str,$str2,$tt){
	$f=strpos($str2,$tt);
	if($f===false){
	//Not find thutu in chapter_name
	$string="$str $str2,$str $str2 tieng viet";
	}else{
	$string="$str  chap $tt,$str $tt,$str chap $tt tieng viet,$str $tt tieng viet,doc truyen $str $tt tieng viet,doc truyen $str chap $tt tieng viet";
	}
	if($string){
					
					
							$tagar=explode(",",$string);
								$tag="";
										foreach($tagar as $value ){
											$value=trim($value);
											$tag.="<a href='".mangaf1_url_manga_tag2(khongdau($value))."'>$value</a> ,";
																								
										}
										
					return substr($tag,0,strlen($tag)-1);
							
				}
}
function mangaf1_tag3_generate($str,$num,$anime_id){

	//Check num delete-----
	$db=new db;
	$db->connect();
	$mnum=$num-2;
	$db->nonquery("delete from anime_tag3 where anime_id=$anime_id and thutu <".$num);
	$num1=$num+1;
	$num2=$num+2;
	$num3=$num+3;
	
	//Tag level 1
	$db->query("select id from anime_tag3 where anime_id=$anime_id and thutu=".$num1);
	if($db->num_rows()==0){
	
	$str1=mangaf1_tag2_generates($str,$num1);
	//echo $str1;
	mangaf1_manga_tag3($num1,$anime_id,$str1);
	}
	//Tag level 2
	$db->query("select id from anime_tag3 where anime_id=$anime_id and thutu=".$num2);
	if($db->num_rows()==0){
	
	$str2=mangaf1_tag2_generates($str,$num2);
	mangaf1_manga_tag3($num2,$anime_id,$str2);
	}
	//Tag level 3
	$db->query("select id from anime_tag3 where anime_id=$anime_id and thutu=".$num3);
	if($db->num_rows()==0){
	
	$str3=mangaf1_tag2_generates($str,$num3);
	mangaf1_manga_tag3($num3,$anime_id,$str3);
	}
	
//	echo mysql_error();
	$db->close();
	
	//Xong roi , thiet la vai l qua di

}

function mangaf1_tag4_link($string){


		if($string){
					
					
							$tagar=explode(",",$string);
								$tag="";
										foreach($tagar as $value ){
											$value=trim($value);
											$tag.="<a href='".mangaf1_url_manga_tag4(khongdau($value))."'>$value</a> ,";
																								
										}
										
					return substr($tag,0,strlen($tag)-1);
							
				}

}

function mangaf1_tag3_link($string){


		if($string){
					
					
							$tagar=explode(",",$string);
								$tag="";
										foreach($tagar as $value ){
											$value=trim($value);
											$tag.="<a href='".mangaf1_url_manga_tag3(khongdau($value))."'>$value</a> ,";
																								
										}
										
					return substr($tag,0,strlen($tag)-1);
							
				}

}

function mangaf1_manga_tag1($id,$str){
	//Insert anime tag
			if($str){
					
						$db=new db;
						$db->connect();
							$tagar=explode(",",$str);
										foreach($tagar as $value ){
											$value=trim($value);
											$tag=khongdau($value);
											if($tag){
											$db->query("select * from anime_tag1 where alias ='".$tag."'");
											
														if($db->num_rows()>0){
															$row=$db->fetchOne();
														
															
															//Handle tag_post
															$db->query('select * from anime_tag1_fk where anime_id='.$id.' and tag_id='.$row['id']);
															
																if($db->num_rows()>0){
																
																}else{
																	
																 	$db->nonquery("insert into anime_tag1_fk(tag_id,anime_id) values(".$row['id'].",".$id.")");
																
																}
														
														}else{
														
															$db->nonquery("insert into anime_tag1(name,alias) values('".$value."','".$tag."')");
														
														
															$tag_id=$db->get_insert_id();
															$db->nonquery("insert into anime_tag1_fk(tag_id,anime_id) values(".$tag_id.",".$id.")");
															
															
														}
											}// Neu tag ko rong
										}
										
						$db->close();
							
				}

}

function mangaf1_manga_tag2($epid,$id,$str){
	//Insert anime tag
			if($str){
					
						$db=new db;
						$db->connect();
							$tagar=explode(",",$str);
										foreach($tagar as $value ){
											$value=trim($value);
											$tag=khongdau($value);
											
											$db->query("select * from anime_tag2 where ep_id=$epid and alias ='".$tag."'");
											
														if($db->num_rows()>0){
															$row=$db->fetchOne();
														
															
															
														
														}else{
														
															$db->nonquery("insert into anime_tag2(name,alias,ep_id,anime_id) values('".$value."','".$tag."',".$epid.",".$id.")");
															
															
															
														}
										}
						$db->nonquery("update chapterk2 set gtag=1 where id=".$epid);				
						$db->close();
							
				}

}

function mangaf1_manga_tag3($thutu,$id,$str){
	//Khong close db de con su dung trong function tag3_generate
	//Insert anime tag
			if($str){
					
						$db=new db;
						$db->connect();
							$tagar=explode(",",$str);
										foreach($tagar as $value ){
											$value=trim($value);
											$tag=khongdau($value);
											
											$db->query("select * from anime_tag3 where alias ='".$tag."'");
											
														if($db->num_rows()>0){
															}else{
												$db->nonquery("insert into anime_tag3(name,alias,thutu,anime_id) values('".$value."','".$tag."',".$thutu.",".$id.")");
												
														}
										}
										
						
							
				}

}





//STATIC ADMIN


function mangaf1_static_admin(){
						$cache=new cache;
						$dt=$cache->load('static','home');
						if(!$dt){
						$admin=0;
						$smod=0;
						$uploader=0;
						$db=new db;
						$db->connect();
						//DUYET VA XOA ANIME
						$db->query("select id from anime where duyet=0");
						$dt['duyet_anime']= $db->num_rows();
						$admin=$admin+$dt['duyet_anime'];
						$smod=$smod+$dt['duyet_anime'];
						$db->query("select id from anime where xoa=1");
						$dt['xoa_anime']=$db->num_rows();
						$admin=$admin+$dt['xoa_anime'];
						//DUYET VA XOA FANSUB
					
						
						
						//XOA EPISODE
						
						$db->query("select id from chapterk2 where xoa=1");
						$dt['xoa_episode']=$db->num_rows();
						$admin=$admin+$dt['xoa_episode'];
						$smod=$smod+$dt['xoa_episode'];
						
					
					
						
						$db->query("select id from report_truyen where xuly=0");
						$dt['reporttruyen']=$db->num_rows();
						$k=	$dt['reporttruyen'];
						$admin=$admin+$k;
						$smod=$smod+$k;
						$uploader=$uploader+$k;
						$db->close();
						$dt['uploader']=$k;
						$dt['smod']=$smod;
						$dt['admin']=$admin;
						$cache->save('static',$dt,300,'home');
						}
						?>
				
                    			<li class="dropdown">
						<a class="btn dropdown-toggle" data-toggle="dropdown" >
							<i class="fa fa-bell-o"></i> Xử Lý <span class="badge"><?php if($_SESSION['user']['level']==3){ echo $dt['uploader'];}else if($_SESSION['user']['level']==2){ echo $dt['smod'];}else{ echo $dt['admin'];} ?></span>
						</a>
							<ul class="dropdown-menu">
                             <?php if($_SESSION['user']['level']==2||$_SESSION['user']['level']==1){ ?>	
            	 <li><a href="<?php echo HOME;?>index.php?module=user&act=anime&type=anime&ext=duyet">Duyệt  Manga <span class="badge"><?php echo $dt['duyet_anime']; ?></span></a></li>

                                           
                                        <?php if($_SESSION['user']['level']==1){ ?>	
                                       				<li><a href="<?php echo HOME;?>index.php?module=user&act=anime&type=anime&ext=xoa">Xóa Manga <span class="badge"><?php echo $dt['xoa_anime']; ?></span></a></li>
                                      
									   <?php }?>
                                       
                                       
                                       
                                       <li><a href="<?php echo HOME; ?>index.php?module=user&act=anime&type=episode&ext=xoa">Xóa Chapter <span class="badge"><?php echo $dt['xoa_episode']; ?></span></a></li>
                                       
                              		 <?php } ?>
                               
                               
                               <li><a href="<?php echo HOME; ?>index.php?module=user&act=anime&type=report&ext=xuly">Report  <span class="badge"><?php echo $dt['reporttruyen']; ?></span></a></li> 
                     				
						</ul>
					</li><?php
}






function mangaf1_manga_theloai($id,$str){
			if($str){
					
						$db=new db;
						$db->connect();
							$tagar=explode(",",$str);
										foreach($tagar as $value ){
											$value=trim($value);
												/*	$tag=intval($value);
													//Handle tag_post
															$db->query('select * from anime_theloai_fk where anime_id='.$id.' and theloai_id='.$tag);
																if($db->num_rows()>0){
																	}else{
																	
																 	$db->nonquery("insert into anime_theloai_fk(theloai_id,anime_id) values(".$tag.",".$id.")");
																	echo mysql_error();
															}
												*/
												//Start code moi
												if($value!=''){
												$tag=khongdau(trim($value));
												$db->query("select * from anime_theloai where alias ='".$tag."'");
											
														if($db->num_rows()>0){
															$row=$db->fetchOne();
														
															
															//Handle tag_post
															$db->query('select * from anime_theloai_fk where anime_id='.$id.' and theloai_id='.$row['id']);
														
																if($db->num_rows()>0){
																
																}else{
																	
																 	$db->nonquery("insert into anime_theloai_fk(theloai_id,anime_id) values(".$row['id'].",".$id.")");
															
																}
														
														}else{
														
														/*	$db->nonquery("insert into anime_theloai(name,alias) values('".$value."','".$tag."')");
															$tag_id=$db->get_insert_id();
															$db->nonquery("insert into anime_theloai_fk(theloai_id,anime_id) values(".$tag_id.",".$id.")");
															*/
															
														}
												}//End of $value!=''
												
												//End code moi
										}
										
						$db->close();
							
				}

}



function mangaf1_manga_theloai2($str){
			if($str){
					
						$db=new db;
						$db->connect();
						$db->query("select id,name from anime_theloai");
						$dt=$db->fetchAll();
						$str2=$str;
						foreach($dt as $row){
							$name=$row['name'];
							if($name!='')
							if(stripos($str,$name)!==false)
							{
								//Find the loai nay
								$dt2[]=trim($name);
								// replace this the loai
								$str2=str_replace($name,"",$str2);
								
							}
							
						}
						
					/*	if(trim($str2)!=''){
								$ar=explode(" ",$str2);
								foreach($ar as $row){
									if($row!=''){
									$dt2[]=trim($row);
									}
								}
						}*/
						
						//$dt2 la mang du lieu do;
						
						if(count($dt2)>0){
						$str=implode(',',$dt2);
						}
						
						$db->close();
			}
				return $str;

}


function mangaf1_manga_theloai_tag($id,$str){
			if($str){
					
						$db=new db;
						$db->connect();
							$tagar=explode(",",$str);
										foreach($tagar as $value ){
											$value=trim($value);
											
												//Start code moi
												if($value!=''){
												$tag=khongdau(trim($value));
											
												$db->query("select * from anime_tag4 where alias ='".$tag."'");
											
														if($db->num_rows()>0){
																										
														}else{
												$db->nonquery("insert into anime_tag4(name,alias,theloai_id) values('".$value."','".$tag."',".$id.")");
														}
												}//End of $value!=''
												
												//End code moi
										}
										
						$db->close();
							
				}

}

function mangaf1_picasa_link($link,$size){
	if(strpos($link,'blogspot.com')!=='false'){
		return str_replace("/s0/","/s$size/",$link);
	}
	return $link;
}
//Manga function ------------

?>